<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/09/17
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Tpmecms;
use think\Db;
use think\Config;

/**
 * 钱包
 */
class Money extends Tpmecms
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['p'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected $db = 'money_order';



    /**
     * 充值钱包🎟
     *
     * @param float $money 充值金额
     * @param string $pay_type 1=微信,2=支付宝,9=其他',
     * @param int $pay_role 1=货主端,2=司机端
     * @return void
     */
    public function recharge_money($money=1.0,$pay_type='1',$pay_role=1)
    {
        if(!$pay_role){
            $pay_role=1;
        }
        
        $data['money'] = $money;
        $data['pay_status'] = '1';//支付状态:1=待支付,2=已支付,3=已退款,4=已超时
        $data['uid'] = $this->auth->id;
        $data['pay_role'] = $pay_role;      //1=货主端,2=司机端
        $data['pay_type'] = $pay_type;//支付类型:1=微信,2=支付宝9=其他

        $res = Db::name($this->db)->where($data)->where('time','>',time()-10)->find();
        if($res){
            $res_data = $this->RechargeMoney($res['id'],$pay_type,$pay_role);
        }
        $data['time'] = time();
        
        $data['order'] = $this->rand_order('Money',$this->auth->id,18);
        $data['id'] = Db::name('money_order')->insertGetId($data);
        $res_data = $this->RechargeMoney($data['id'],$pay_type,$pay_role);
        $this->success($res_data,$res_data);//返回支付的参数
    }





    /**
     * 充值钱包
     * 
     * @ApiInternal()
     * @param string $order_id 订单id
     * @param string $pay_type 支付类型:1=微信,2=支付宝,9=其他
     * @param int $pay_role 1=货主端,2=司机端
     * @return void
     */
    protected function RechargeMoney($order_id,$pay_type = '1',$pay_role=1)
    {
  
              
        $where_order['id'] = $order_id;
        // $where_order['uid'] = $uid;
        

       
        //查询订单
        $money_order = Db::name('money_order')->where($where_order)->find();
     
        if(!$money_order){
            $this->error('没有该订单信息');
        }
    
        //如果支付不相同就修改 
        if( $money_order['pay_type'] != $pay_type){
            Db::name('pay_order')->where('id',$money_order['id'])->update(['pay_type'=>$pay_type]);
        }
    
        //支付
        $Pay = new \app\api\controller\Pay($pay_role);
        // halt($Pay->_params);
        switch ($pay_type) {
            case '1'://微信
                return $Pay->wx_pay_money_order($money_order); 
                break;
            case '2'://支付宝
                return $Pay->ali_pay_money_order($money_order); 
                break;
     
            default:
                $this->error('暂未开通此项支付');
                break;
        }
        
       
    }


    /**
     * 获取充值列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $pay_status 可不传)支付状态:1=待支付,2=已支付,3=已退款,4=已超时
     * @return void
     */
    public function get_recharge_money_list($page = 1,$limit=10 ,$pay_status=null)
    {
        $where['uid'] = $this->auth->id;
        if($pay_status){
            $where['pay_status'] = $pay_status;
        }

        $list = Db::name('money_order')->where($where)->page($page,$limit)->select();


        foreach ($list as $key => &$value) {
            $value['date'] = date('Y-m-d H:i:s',$value['pay_time']);
        }
        $this->success('ok',$list);
    }

    /**
     * 支付未支付的订单
     *
     * @param int $id 订单id
     * @param string $pay_type 支付类型:1=微信,2=支付宝,9=其他
     * @return void
     */
    public function pay_un_recharge_money($id,$pay_type='1')
    {
        $data['id'] = $id;
        $data['uid'] = $this->auth->id;
        $res = Db::name($this->db)->where($data)->find();
        if($res){
            $res_data = $this->RechargeMoney($res['id'],$pay_type);
        }else{
            $this->error('没有该订单');
        }

        $this->success('ok',$res_data);//返回支付的参数
    }


    /**
     * 使用钱包记录💰
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function user_money_log($page=1,$limit=10)
    {
       $list = Db::name('user_money_log')   
            ->where('user_id',$this->auth->id) 
            ->order('createtime desc')
            ->page($page,$limit) 
            ->select();
        foreach ($list as $key => &$value) {
            $value['date'] = date('Y-m-d H:i:s',$value['createtime']);
        }
       $this->success('ok',$list);
    }

}
